<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="Nordic的引脚是可以自由定义的。只有SAADC接口是固定的那几个引脚，数字引脚，PWM, I2C, UART, I2S都是可以自由定义的。">
<meta property="og:type" content="article">
<meta property="og:title" content="nRF52832开发记录-pwm实现呼吸灯">
<meta property="og:url" content="http://example.com/2020/06/08/nRF52832%E5%BC%80%E5%8F%91%E8%AE%B0%E5%BD%95-pwm%E5%AE%9E%E7%8E%B0%E5%91%BC%E5%90%B8%E7%81%AF/index.html">
<meta property="og:site_name" content="webuildthings">
<meta property="og:description" content="Nordic的引脚是可以自由定义的。只有SAADC接口是固定的那几个引脚，数字引脚，PWM, I2C, UART, I2S都是可以自由定义的。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-06-08T11:50:10.000Z">
<meta property="article:modified_time" content="2020-09-02T03:43:06.582Z">
<meta property="article:author" content="wusk">
<meta property="article:tag" content="BLE">
<meta property="article:tag" content="Nordic">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/2020/06/08/nRF52832%E5%BC%80%E5%8F%91%E8%AE%B0%E5%BD%95-pwm%E5%AE%9E%E7%8E%B0%E5%91%BC%E5%90%B8%E7%81%AF/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>nRF52832开发记录-pwm实现呼吸灯 | webuildthings</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">webuildthings</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">遇酒且呵呵 人生能几何</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2020/06/08/nRF52832%E5%BC%80%E5%8F%91%E8%AE%B0%E5%BD%95-pwm%E5%AE%9E%E7%8E%B0%E5%91%BC%E5%90%B8%E7%81%AF/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="wusk">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="webuildthings">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          nRF52832开发记录-pwm实现呼吸灯
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-06-08 19:50:10" itemprop="dateCreated datePublished" datetime="2020-06-08T19:50:10+08:00">2020-06-08</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-09-02 11:43:06" itemprop="dateModified" datetime="2020-09-02T11:43:06+08:00">2020-09-02</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/nRF52832/" itemprop="url" rel="index"><span itemprop="name">nRF52832</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/nRF52832/BLE/" itemprop="url" rel="index"><span itemprop="name">BLE</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>Nordic的引脚是可以自由定义的。只有SAADC接口是固定的那几个引脚，<br>数字引脚，PWM, I2C, UART, I2S都是可以自由定义的。    </p>
<a id="more"></a> 

<h1 id="1-使用能PWM"><a href="#1-使用能PWM" class="headerlink" title="1. 使用能PWM"></a>1. 使用能PWM</h1><p>配置<code>sdk_config.h</code>，使能PWM    </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">#define NRFX_PWM_ENABLED 1</span><br><span class="line">#define PWM_ENABLED 1</span><br><span class="line">#define PWM0_ENABLED 1</span><br><span class="line">#define NRFX_PWM0_ENABLED 0</span><br></pre></td></tr></table></figure>

<h1 id="2-实例化"><a href="#2-实例化" class="headerlink" title="2. 实例化"></a>2. 实例化</h1><p>总共支持4个PWM，分别是PWM0, PWM1, PWM2, PWM3 .<br>使用PWM0<br><code>static nrf_drv_pwm_t m_pwm0 = NRF_DRV_PWM_INSTANCE(0);</code></p>
<h1 id="3-初始化"><a href="#3-初始化" class="headerlink" title="3. 初始化"></a>3. 初始化</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">static void pwmInit(void)</span><br><span class="line">&#123;    </span><br><span class="line"></span><br><span class="line">    nrf_drv_pwm_config_t const config0 &#x3D;</span><br><span class="line">    &#123;</span><br><span class="line">        .output_pins &#x3D;</span><br><span class="line">        &#123;</span><br><span class="line">            10 | NRF_DRV_PWM_PIN_INVERTED, &#x2F;&#x2F; channel 0</span><br><span class="line">            NRF_DRV_PWM_PIN_NOT_USED, &#x2F;&#x2F; channel 1</span><br><span class="line">            NRF_DRV_PWM_PIN_NOT_USED, &#x2F;&#x2F; channel 2</span><br><span class="line">            NRF_DRV_PWM_PIN_NOT_USED  &#x2F;&#x2F; channel 3</span><br><span class="line">        &#125;,</span><br><span class="line">        .irq_priority &#x3D; APP_IRQ_PRIORITY_LOWEST,</span><br><span class="line">        .base_clock   &#x3D; NRF_PWM_CLK_1MHz,</span><br><span class="line">        .count_mode   &#x3D; NRF_PWM_MODE_UP,</span><br><span class="line">        .top_value    &#x3D; 10000,</span><br><span class="line">        .load_mode    &#x3D; NRF_PWM_LOAD_INDIVIDUAL,</span><br><span class="line">        .step_mode    &#x3D; NRF_PWM_STEP_AUTO</span><br><span class="line">    &#125;;</span><br><span class="line">    APP_ERROR_CHECK(nrf_drv_pwm_init(&amp;m_pwm0, &amp;config0, pwm_red_handler));    </span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h1 id="4-回调函数-呼吸效果实现"><a href="#4-回调函数-呼吸效果实现" class="headerlink" title="4. 回调函数,呼吸效果实现"></a>4. 回调函数,呼吸效果实现</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">static int gTmpRedCnt &#x3D; 0;</span><br><span class="line">static void pwm_red_handler(nrf_drv_pwm_evt_type_t event_type)</span><br><span class="line">&#123;</span><br><span class="line">    if (event_type &#x3D;&#x3D; NRF_DRV_PWM_EVT_FINISHED)</span><br><span class="line">    &#123;</span><br><span class="line"></span><br><span class="line">        uint16_t * p_channels &#x3D; (uint16_t *)&amp;(gRedSeqValues.channel_0);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        &#x2F;&#x2F; 呼吸效果(-5000, 5000对应 top_value&#x3D;10000)</span><br><span class="line">        *p_channels &#x3D; (-5000) * cos(3.14&#x2F;1500 * (gTmpRedCnt)) + 5000;  </span><br><span class="line">        gTmpRedCnt +&#x3D; 10;</span><br><span class="line"></span><br><span class="line">        if(gTmpRedCnt &gt;&#x3D; 0xFFFFFF00)&#123;</span><br><span class="line">            gTmpRedCnt &#x3D; 0;        </span><br><span class="line">        &#125;        </span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h1 id="5-运行"><a href="#5-运行" class="headerlink" title="5. 运行"></a>5. 运行</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">static nrf_pwm_values_individual_t gRedSeqValues&#x3D;&#123;0&#125;;</span><br><span class="line">static nrf_pwm_sequence_t const    gRedSeq &#x3D;</span><br><span class="line">&#123;</span><br><span class="line">    .values.p_individual &#x3D; &amp;gRedSeqValues,</span><br><span class="line">    .length              &#x3D; NRF_PWM_VALUES_LENGTH(gRedSeqValues),</span><br><span class="line">    .repeats             &#x3D; 0,</span><br><span class="line">    .end_delay           &#x3D; 0</span><br><span class="line">&#125;;</span><br><span class="line">nrf_drv_pwm_simple_playback(&amp;m_pwm0, &amp;gRedSeq, 1,NRF_DRV_PWM_FLAG_LOOP);</span><br></pre></td></tr></table></figure>

<h1 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h1><ul>
<li><a target="_blank" rel="noopener" href="https://www.jianshu.com/p/76f0cd558710">蓝牙芯片nRF52832之PWM的使用</a></li>
<li><a target="_blank" rel="noopener" href="https://blog.csdn.net/leirifa/article/details/86582593">nRF52 PWM 使用</a></li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/BLE/" rel="tag"><i class="fa fa-tag"></i> BLE</a>
              <a href="/tags/Nordic/" rel="tag"><i class="fa fa-tag"></i> Nordic</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/06/07/nRF52832%E5%BC%80%E5%8F%91%E8%AE%B0%E5%BD%95-nRFConnect-PC-%E4%B8%8D%E8%83%BD%E6%B7%BB%E5%8A%A0app/" rel="prev" title="nRF52832开发记录-nRFConnect PC 不能添加app">
      <i class="fa fa-chevron-left"></i> nRF52832开发记录-nRFConnect PC 不能添加app
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/06/12/nRF52832%E5%BC%80%E5%8F%91%E8%AE%B0%E5%BD%95-%E8%93%9D%E7%89%99%E6%8A%93%E5%8C%85/" rel="next" title="nRF52832开发记录-蓝牙抓包">
      nRF52832开发记录-蓝牙抓包 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-%E4%BD%BF%E7%94%A8%E8%83%BDPWM"><span class="nav-number">1.</span> <span class="nav-text">1. 使用能PWM</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-%E5%AE%9E%E4%BE%8B%E5%8C%96"><span class="nav-number">2.</span> <span class="nav-text">2. 实例化</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-number">3.</span> <span class="nav-text">3. 初始化</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#4-%E5%9B%9E%E8%B0%83%E5%87%BD%E6%95%B0-%E5%91%BC%E5%90%B8%E6%95%88%E6%9E%9C%E5%AE%9E%E7%8E%B0"><span class="nav-number">4.</span> <span class="nav-text">4. 回调函数,呼吸效果实现</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#5-%E8%BF%90%E8%A1%8C"><span class="nav-number">5.</span> <span class="nav-text">5. 运行</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8F%82%E8%80%83"><span class="nav-number">6.</span> <span class="nav-text">参考</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">wusk</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">34</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
        <span class="site-state-item-count">3</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">wusk</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
